﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Huasky.Jwt.Controllers;

[ApiController]
[Route("[controller]")]
public class HomeController(IHttpContextAccessor httpContextAccessor)
{
    private HttpContext _context { get; set; } = httpContextAccessor.HttpContext;

    [HttpGet("[action]")]
    public string Login()
    {
        var user = new JwtUserInfo { Id = 122, Code = "sunxiaoshuang", Name = "孙小双", Role = "cashier,admin" };
        var token = JwtHelper.IssueJwt(user);
        return token;
    }

    [HttpGet]
    public string GetTime() => DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

    [Authorize]
    [HttpGet("[action]")]
    public string GetStudent()
    {
        var name = _context.User.Claims.FirstOrDefault(a => a.Type == "name").ValueType;
        var code = _context.User.Claims.FirstOrDefault(a => a.Type == "code");
        var id = _context.User.Claims.FirstOrDefault(a => a.ValueType == "id");
        return $"student: {name}, {code}";
    }


    [Authorize("Admin")]
    [HttpGet("[action]")]
    public string GetSchool() => "school";


    [Authorize("Cashier")]
    [HttpGet("[action]")]
    public string GetGrade() => "grade";


    [Authorize("AdminOrCashier")]
    [HttpGet("[action]")]
    public string GetResult() => "result";


    [Authorize("AdminAndCashier")]
    [HttpGet("[action]")]
    public string GetFather() => "father";


}
